/**
 * 
 */
package org.corpus.english.controller;

import java.util.Iterator;
import java.util.List;

import org.corpus.english.model.SentenceSourceData;
import org.corpus.english.model.SentenceSourceDataHome;
import org.corpus.english.view.SentenceSourceTreeNode;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;


/**
 * @author robbiecheng
 *
 */
public class SentenceSourceAdminController extends GenericForwardComposer{
	private SentenceSourceDataHome ssdDao = new SentenceSourceDataHome();
	private SentenceSourceData currentSSD = new SentenceSourceData();
	private String newSource = null;
	
	public String getNewSource() {
		return newSource;
	}

	public void setNewSource(String newSource) {
		this.newSource = newSource;
	}

	public SentenceSourceData getCurrentSSD(){
		return currentSSD;
	}
	
	//save into current source
	public void onSelect$source(SelectEvent evt){
		currentSSD = (SentenceSourceData)((SentenceSourceTreeNode)((Treeitem)evt.getReference()).getValue()).getValue();
	}
	//insert a new sub-source
	private Tree source;
	public void onClick$save(){		
		if (currentSSD.getSourceid() == 0)
			return;
		
		//save into database
		SentenceSourceData newSSD = new SentenceSourceData();
		newSSD.setParentid(currentSSD.getSourceid());
		newSSD.setName(newSource.trim());
		String fullname;
		if (currentSSD.getSourceid() == 1)
			fullname = newSource;
		else
			fullname = currentSSD.getFullname()+ "-" + newSource;
		newSSD.setFullname(fullname);
		ssdDao.persist(newSSD);	
		((SentenceSourceTreeNode)source.getSelectedItem().getValue()).readChildren();
		newCurrentSCD();
	}

	//update source
	public void onClick$update(){
		if (currentSSD.getSourceid() <= 1)
			return;
		SentenceSourceData parent = ssdDao.findById(currentSSD.getParentid());
		String oldFullname = currentSSD.getFullname();
		String newFullname = currentSSD.getParentid() == 1 ? currentSSD.getName() :
			parent.getFullname() + "-" + currentSSD.getName().trim();
		
		//update currentSSD
		currentSSD.setFullname(newFullname);		
		ssdDao.merge(currentSSD);
		
		//update all child SSD
		List children = ssdDao.findByFullName(oldFullname + "-");
		for (Iterator iterator = children.iterator(); iterator.hasNext();) {
			SentenceSourceData data = (SentenceSourceData) iterator.next();
			String newFn = newFullname + "-" + data.getName();
			data.setFullname(newFn);
			ssdDao.merge(data);
		}
		reloadSelectedSSD();
	}
	
	//delete source
	public void onClick$delete(){
		if (currentSSD.getSourceid() == 0)
			return;
		
		ssdDao.delete(currentSSD);
		reloadSelectedSSD();
	}

	private void reloadSelectedSSD() {
		((SentenceSourceTreeNode)source.getSelectedItem().getParentItem().getValue()).readChildren();
		newCurrentSCD();
	}
	
	private void newCurrentSCD() {
		currentSSD = new SentenceSourceData();
		
	}
}
